<template>
  <div class="main-app">
    <el-form ref="form" :model="form" :rules="rules" label-width="600px" style="margin-top: 50px;">
      <el-form-item label="区域代号:" prop="code">
        <el-input v-model="form.code" clearable style="width: 50%;" :disabled="isEdit"/>
      </el-form-item>
      <el-form-item label="状态:" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio
            v-for="(item, index) in options"
            :key="index"
            :label="item.value"
          >{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="区域名称:" prop="name">
        <el-input v-model="form.name" clearable style="width: 50%;" />
      </el-form-item>
    </el-form>
    <el-row style="margin-top: 40px; text-align: center">
      <el-form :inline="true" class="form-inline">
        <el-form-item>
          <el-button type="primary" @click="save">提交</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-row>
  </div>
</template>

<script>
import { EquipmentStatus } from "../../../utils/resource";
import { save, getById } from "../../../api/system";
export default {
  data() {
    return {
      form: {
        code: "",
        status: "0",
        name: "",
      },
      options: EquipmentStatus,
      isEdit:false,
      rules: {
        code: [{ required: true, message: "请输入区域代号", trigger: "blur" }],
        name: [{ required: true, message: "请输入区域名称", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      id: "",
    };
  },
  async created() {
    const { id,type } = this.$route.params;
    await this.getById(id,type);
    this.id = id;
    if(id){
      this.isEdit=true;
    }else{
       this.isEdit=false;
    }
  },
  methods: {
    async getById(id,type) {
      const { data } = await getById({ type: type, code: id });
      this.form.name = data.value;
      this.form.code = data.code;
      this.form.status = data.status.toString();

    },
    save() {
      this.$refs["form"].validate(async(valid) => {
        if (valid) {

          const data = {
            type: "100",
            code: this.form.code,
            status: this.form.status,
            value: this.form.name,

          };
          let message = "";
          if (!this.id) {
            data.isAdd="1";
			  // data = Object.assign({ isAdd: "1" }, data);
            message = "广西省城市代码新增成功";
          } else {
            message = "广西省城市代码修改成功";
          }
console.log(data);
          const { success, msg } = await save(data);
          if (success) {
            this.$notify({
              title: "成功",
              message: message,
              type: "success",
              duration: 3000,
            });
            this.$router.push("/system/citycode");
          } else {
            this.$notify.error({
              title: "失败",
              message: msg,
              duration: 3000,
            });
          }
        }
      });
    },
    cancel() {
      this.$router.push("/system/citycode");
    },
  },
};
</script>
